package com.dalonedrau.jogl.opengl.ui.border;

import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GlobalConsts;
import com.dalonedrau.jogl.engine.JOGLErrorHandler;
import com.dalonedrau.jogl.engine.Render;
import com.dalonedrau.jogl.opengl.sprite.BasicQuadSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.BasicSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.GroupOfImages;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.vectors.Vector3;

/**
 * A simple border, similar to those found in the early 90's SNES games.
 * @author Donald
 */
public final class GradientBackgroundBorder extends GuiBorder {
	/** the middle sprite. */
	private BasicQuadSpriteObject	middle;
	/** Creates a new instance of {@link GradientBackgroundBorder}. */
	public GradientBackgroundBorder() { }
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(middle);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(final double x, final double y, final double z) {
		super.setPosition(x, y, z);
		middle.setPosition(x + 3, y + 3, z);
		middle.clearFloatStore();
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPreferredSize(final int width, final int height) {
		super.setPreferredSize(width, height);
		try {
			if (middle == null) {
				{ // middle
					middle = new BasicQuadSpriteObject();
					middle.setRed(GlobalConsts.COL_SNES_BORDER[0]);
					middle.setGreen(GlobalConsts.COL_SNES_BORDER[1]);
					middle.setBlue(GlobalConsts.COL_SNES_BORDER[2]);
					middle.setAlpha(GlobalConsts.COL_SNES_BORDER[GlobalConsts.INT_003]);
				}
			}
			// set middle position and width
			middle.setWidth(width - 6);
			middle.setHeight(height - 6);
			if (super.getPosition() != null) {
				middle.setPosition(
						super.getPosition().getX() + 3,
						super.getPosition().getY() + 3,
						super.getPosition().getZ()
						);
				middle.clearFloatStore();
			}
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
}
